/**
 * 便于统一处理支付请求功能
 */

export default function (this_, order) {
  this_.$axios.post("api/public/order/payInfo", order).then(response => {
    let infoResp = response.body;
    if (!infoResp['flag']) {
      this.submitEnd();
      this_.$vux.toast.show({
        text: "支付时发生异常，原因:" + resp["message"],
        type: 'warn',
        width: '25em'
      });
      return;
    }
    //调用微信支付
    let pay = infoResp.wxResp;
    let request = {
      "appId": pay.appId,     //公众号名称，由商户传入
      "timeStamp": pay.timestamp,         //时间戳，自1970年以来的秒数
      "nonceStr": pay.nonceStr, //随机串
      "package": pay.packageValue,
      "signType": "MD5",         //微信签名方式：
      "paySign": pay.paySign//微信签名
    };
    if (!window.WeixinJSBridge) {
      this.submitEnd();
      this.$vux.toast.show({
        text: "请在微信环境中进行支付!",
        width: '25em'
      });
      return;
    }
    this.hide();
    window.WeixinJSBridge.invoke(
      'getBrandWCPayRequest', request,
      (res) => {
        if (res.err_msg === "get_brand_wcpay_request:ok") {
          //调用后端确认接口，确认购买的数量
          let req = {openId: this.openId, vin: this.vin, orderNo: infoResp.orderNum, mobile: this.mobile};
          this.$axios.post("api/public/order/orderPayConfirm", req).then(respose => {
            this.$router.push("/orderList");
          }).catch(response => {
            this.$router.push("/orderList");
          });
        } else {
          this.$vux.toast.show({
            text: "支付失败，请在我的订单当中重新支付!",
            type: 'warn',
            width: '25em'
          });
          this.$router.push("/orderList");
        }
      });
  }).catch(response => {
    this.submitEnd();
    let code = response.code;
    let msg = baseSubmitMsg[code];
    if (msg) {
      this.$vux.toast.show({
        text: msg,
        width: '25em'
      })
    } else {
      let message = response.message;
      this.$vux.toast.show({
        text: message,
        type: 'warn',
        width: '25em'
      })
    }
  });
};
